namespace SimpleWebServices.Models
{
    using System.Globalization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class PersonalNameFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<PersonalName>()
                .DerivesFrom<ValueObject<PersonalName>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("name")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            PersonalName actual = "<name salutation='Mr' given='John' family='Doe' />".Deserialize<PersonalName>();

            Assert.NotNull(actual);

            Assert.Equal<string>("Mr", actual.Salutation);
            Assert.Equal<string>("John", actual.Given);
            Assert.Equal<string>("Doe", actual.Family);
        }

        [Fact]
        public void deserializeEmpty()
        {
            PersonalName actual = "<name />".Deserialize<PersonalName>();

            Assert.NotNull(actual);

            Assert.Null(actual.Salutation);
            Assert.Null(actual.Given);
            Assert.Null(actual.Family);
        }

        [Fact]
        public void serialize()
        {
            PersonalName obj = new PersonalName
            {
                Salutation = "Mr",
                Given = "John",
                Family = "Doe"
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/name[@salutation='Mr'][@given='John'][@family='Doe'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new PersonalName());
        }

        [Fact]
        public void prop_Family()
        {
            Assert.True(new PropertyExpectations(typeof(PersonalName).GetProperty("Family"))
                .XmlAttribute("family")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Given()
        {
            Assert.True(new PropertyExpectations(typeof(PersonalName).GetProperty("Given"))
                .XmlAttribute("given")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Salutation()
        {
            Assert.True(new PropertyExpectations(typeof(PersonalName).GetProperty("Salutation"))
                .XmlAttribute("salutation")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void op_ToString_IFormatProvider()
        {
            PersonalName obj = new PersonalName
            {
                Salutation = "Mr",
                Given = "John",
                Family = "Doe"
            };

            string expected = "Mr John Doe";
            string actual = obj.ToString(CultureInfo.InvariantCulture);

            Assert.Equal<string>(expected, actual);
        }
    }
}